package com.koron.order.patrol.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.patrol.bean.po.PatrolEquipmentParamsPo;
import com.koron.order.patrol.mapper.PatrolEquipmentParamsMapper;
import com.koron.order.patrol.service.PatrolEquipmentParamsService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 巡视工单-巡视点设备下参数从表(PatrolEquipmentParams)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-26 15:27:03
 */
@Service("patrolEquipmentParamsService")
public class PatrolEquipmentParamsServiceImpl implements PatrolEquipmentParamsService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, PatrolEquipmentParamsPo patrolEquipmentParams) {

        PatrolEquipmentParamsMapper mapper = factory.getMapper(PatrolEquipmentParamsMapper.class);

        return mapper.insertOrUpdate(patrolEquipmentParams);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<PatrolEquipmentParamsPo> patrolEquipmentParamsList) {

        if (CollectionUtils.isEmpty(patrolEquipmentParamsList)) {
            return 0;
        }

        //Assert.notEmpty(patrolEquipmentParamsList, "参数项不能为空");

        List<String> patrolEquipmentIds = patrolEquipmentParamsList.stream().map(PatrolEquipmentParamsPo::getPatrolEquipmentId).distinct().collect(Collectors.toList());

        Assert.notEmpty(patrolEquipmentIds, "设备id不能为空");

        PatrolEquipmentParamsMapper mapper = factory.getMapper(PatrolEquipmentParamsMapper.class);

        //先根据 巡视工单ids删除
        mapper.deleteByPatrolEquipmentIds(patrolEquipmentIds);

        return mapper.insertBatch(patrolEquipmentParamsList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, PatrolEquipmentParamsPo patrolEquipmentParams) {

        PatrolEquipmentParamsMapper mapper = factory.getMapper(PatrolEquipmentParamsMapper.class);

        return mapper.insert(patrolEquipmentParams);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<PatrolEquipmentParamsPo> patrolEquipmentParamsList) {

        PatrolEquipmentParamsMapper mapper = factory.getMapper(PatrolEquipmentParamsMapper.class);

        return mapper.insertBatch(patrolEquipmentParamsList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, PatrolEquipmentParamsPo patrolEquipmentParams) {
        PatrolEquipmentParamsMapper mapper = factory.getMapper(PatrolEquipmentParamsMapper.class);
        return mapper.update(patrolEquipmentParams);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        PatrolEquipmentParamsMapper mapper = factory.getMapper(PatrolEquipmentParamsMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public PatrolEquipmentParamsPo queryById(SessionFactory factory, String id) {

        PatrolEquipmentParamsMapper mapper = factory.getMapper(PatrolEquipmentParamsMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<PatrolEquipmentParamsPo> queryList(SessionFactory factory, PatrolEquipmentParamsPo patrolEquipmentParams) {

        PatrolEquipmentParamsMapper mapper = factory.getMapper(PatrolEquipmentParamsMapper.class);

        List<PatrolEquipmentParamsPo> list = mapper.queryList(patrolEquipmentParams);

        return list;
    }

    @Override
    @TaskAnnotation("queryByPatrolEquipmentId")
    public List<PatrolEquipmentParamsPo> queryByPatrolEquipmentId(SessionFactory factory, String patrolEquipmentId) {
        PatrolEquipmentParamsPo patrolEquipmentParamsPo = new PatrolEquipmentParamsPo();
        patrolEquipmentParamsPo.setPatrolEquipmentId(patrolEquipmentId);
        return queryList(factory, patrolEquipmentParamsPo);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, PatrolEquipmentParamsPo patrolEquipmentParams, PageQuery pageQuery) {

        if (CollUtil.isNotEmpty(pageQuery.getOrders())) {
            pageQuery.prefixOrders("patrolEquipmentParams", null);
        }
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());

        PatrolEquipmentParamsMapper mapper = factory.getMapper(PatrolEquipmentParamsMapper.class);

        List<PatrolEquipmentParamsPo> list = mapper.queryList(patrolEquipmentParams);

        return new PageInfo(list);
    }

}